package com.smfx.yxt.controller.student;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.InterestCourseStatus;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.InterestCourseStudentSign;
import com.smfx.yxt.model.ReqInterestCourseChange;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.InterestCourseQueryVO;
import com.smfx.yxt.reqVo.ReqInterestCourseQueryVO;
import com.smfx.yxt.service.IInterestCourseInfoService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.view.InterestCourseStudentSignView;

@RequestMapping(Const.STUDENT_ROLE + MenuConst.INTEREST_COURSE)
@Controller
public class ChooseCourseControoller extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(ChooseCourseControoller.class);
	
	@Autowired
	private IInterestCourseInfoService interestCourseInfoService;
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value={"","/"})
	public String index(Model model) {
		AppUser appUser = getAppUser();
		InterestCourseQueryVO vo = new InterestCourseQueryVO();
		vo.setTermId(getCurrentTerm().getId());
		vo.setParentId(appUser.getParent().getId());
		List<InterestCourseStudentSignView> list = interestCourseInfoService.selectSignListForParent(vo);
		model.addAttribute("list", list);
		return "interestCourse/studentIndex";
	}
	
	@RequestMapping("list")
	public String list(Model model) {
		return "interestCourse/studentList";
	}
	
	@RequestMapping("list/query")
	@ResponseBody Object listQuery(InterestCourseQueryVO vo) {
		AppUser appUser = getAppUser();
		StudentInfo studentInfo = studentService.selectStudentBindParent(vo.getStudentId(), appUser.getOpenid());
		if(studentInfo == null) {
			return null;
		}
		vo.setClazz(studentInfo.getClazz());
		vo.setGrade(studentInfo.getGrade());
		vo.setTermId(getCurrentTerm().getId());
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setKey(vo.getCourseId() == null ? null : String.valueOf(vo.getCourseId()));
		return interestCourseInfoService.selectViewListForClass(vo);
	}
	
	@RequestMapping("detail/{courseId}")
	public String detail(Model model, @PathVariable Integer courseId) {
		//学生ID
		String xbsid = request.getParameter("xbsid");
		//已报名的课程id
		String xboid = request.getParameter("xboid");
		if(StringUtils.isNotBlank(xboid)) {
			InterestCourseQueryVO queryVO = new InterestCourseQueryVO();
			queryVO.setTermId(getCurrentTerm().getId());
			queryVO.setStudentId(Integer.valueOf(xbsid));
			queryVO.setCourseId(Integer.valueOf(xboid));
			InterestCourseStudentSignView signView = interestCourseInfoService.selectSignedCourseForStudent(queryVO);		
			if(signView == null) return null;
			model.addAttribute("signView", signView);
		}
		BaseQueryVO vo = new BaseQueryVO();
		vo.setId(courseId);
		vo.setTermId(getCurrentTerm().getId());
		model.addAttribute("detail", interestCourseInfoService.selectViewById(vo));
		return "interestCourse/studentDetail";
	}
	
	@RequestMapping("signSubmit")
	@ResponseBody Object signSubmit(InterestCourseStudentSign record) {
		try {
			record.setTermId(getCurrentTerm().getId());
			interestCourseInfoService.insertSign(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---signSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("changeSubmit")
	@ResponseBody Object changeSubmit(ReqInterestCourseChange record) {
		try {
			AppUser appUser = getAppUser();
			record.setSchoolId(appUser.getParent().getSchoolId());
			record.setTermId(getCurrentTerm().getId());
			record.setApplyerId(appUser.getParent().getId());
			BaseQueryVO vo = new BaseQueryVO();
			vo.setTermId(getCurrentTerm().getId());
			vo.setId(record.getNewCourseId());
			if(interestCourseInfoService.isFullForCourseId(vo)) {
				return new RspResult(Status.COURSE_FULL);
			}
			ReqInterestCourseQueryVO rcVO = new ReqInterestCourseQueryVO();
			rcVO.setTermId(getCurrentTerm().getId());
			rcVO.setStudentId(record.getStudentId());
			rcVO.setStatus(InterestCourseStatus.AUDIT_PENING.getCode());
			int count = interestCourseInfoService.selectCountForStudent(rcVO);
			if(count > 0) {
				return new RspResult(Status.COURSE_CHANGE_PENDING);
			}
			rcVO.setStatus(InterestCourseStatus.AUDIT_SUCCESS.getCode());
			count = interestCourseInfoService.selectCountForStudent(rcVO);
			if(count >= 2) {
				return new RspResult(Status.COURSE_CHANGE_FULL);
			}
			StudentInfo student = studentService.selectById(record.getStudentId());
			if(student == null) {
				return new RspResult(Status.EXPIRED_DATA);
			}
			record.setStudentName(student.getName());
			interestCourseInfoService.addChangeCourseReqByParent(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---signSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("changeClass/req")
	@ResponseBody Object changeClassReq(ReqInterestCourseQueryVO vo) {
		AppUser appUser = getAppUser();
		vo.setParentId(appUser.getParent().getId());
		vo.setTermId(getCurrentTerm().getId());
		return interestCourseInfoService.selectListForParent(vo);
	}
	
	@RequestMapping("changeClass/num")
	@ResponseBody Object changeClassNum(ReqInterestCourseQueryVO vo) {
		vo.setTermId(getCurrentTerm().getId());
		return interestCourseInfoService.selectCountForStudent(vo);
	}
	
}
